<template>
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="4"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="jzySeries"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button  v-show="dealerType !== '25381002'" size="small" type="primary" :auth-list="auths" auth-mark="/api/basedata/vehicleProduct/brands" :click="addHandle">新增</multiuse-button >
        <multiuse-button  size="small" type="primary" icon="el-icon-download"  :auth-list="auths" auth-mark="/api/2"  :click="exportHandle">下载</multiuse-button >
        <!-- <el-button size="small">下载</el-button>
        <el-button size="small">导入</el-button> -->
      </template>
    </FilterTable>
    <BaseDialog
      :visible.sync="visible"
      destroyOnClose
      :title="title"
      width="30%"
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <SeriesModal v-if="visible" :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getBrandListOEM, getSeriesOEM,getSeriesListOEM,exportSeriess } from '@/api/common/cars/brandcarsModels';
import SeriesModal from './seriesModal';
import { exportExcelFile } from '@/utils';
import Cookies from 'js-cookie';
import { authority } from '@/utils/authMixin';
export default {
  name: 'Series',
  mixins: [dictionary,authority],
  components: {
    SeriesModal
  },
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchApi: getSeriesOEM,
      fetchParams: {},
      visible: false,
      handleType: '',
      formData: {},
      title:"新增",
      dealerType: Cookies.get('dealerType'),
      exportLoading: false
    };
  },
  created() {
    this.getDictData();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'SELECT',
          label: '品牌名称',
          fieldName: 'brandId',
          placeholder: '请选择...',
          itemList: [],
          change: val => {
            this.changeBrandSelect(val);
          }
        },
        {
          type: 'INPUT',
          label: '车系名称',
          fieldName: 'seriesName',
          placeholder: '请输入...'
          
        },
        {
          type: 'INPUT',
          label: '车系代码',
          fieldName: 'seriesCode',
          placeholder: '请输入...'
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择...',
          itemList: this.createDictList('1004')
        }
      ];
    },
    // 导出
    async exportHandle(row) {
      let params = JSON.parse(JSON.stringify(this.fetchParams));
      this.handleType = 'export';
      this.exportLoading = true;
      await exportSeriess({ ...params, userId: this.userId })
        .then(response => {
          // console.log(response,"response")
          exportExcelFile(response.data, '车系信息');
          this.exportLoading = false;
        })
        .catch(err => {
          this.exportLoading = false;
          console.log(err);
        });
    },
    createTableColumns() {
      let dealerType = Cookies.get('dealerType');
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 60,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button containerStyle={dealerType !== '25381002' ? {}:{display:'none'}} size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '品牌名称',
          dataIndex: 'brandName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系代码',
          dataIndex: 'seriesCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否有效',
          dataIndex: 'isValid',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isValid, '1004')}</span>;
          }
        }
      ];
    },
    async getDictData() {
      const res = await getBrandListOEM();
      if (res.resultCode === 200) {
        this.topFilterList.find(item=>item.fieldName==='brandId').itemList = res.data.map(x => ({ value: x.brandId, text: x.brandName }));
      }

    },
    async changeBrandSelect(brandsid) {
      if (!brandsid) {
        this.topFilterList.find(item=>item.fieldName==='seriesId').initialValue = '';
        this.topFilterList.find(item=>item.fieldName==='seriesId').itemList = [];
      } else {
        const res = await getSeriesListOEM(brandsid);
        if (res.resultCode === 200) {
          this.topFilterList.find(item=>item.fieldName==='seriesId').initialValue = '';
          this.topFilterList.find(item=>item.fieldName==='seriesId').itemList = res.data.map(x => ({ value: x.seriesId, text: x.seriesName }));
        }
      }
    },
    addHandle() {
      this.handleType = 'add';
      this.visible = true;
      this.title="新增"
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.visible = true;
      this.title="编辑"
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  }
};
</script>

<style lang="less" scoped>
</style>
